package com.neusoft.controller;

import com.neusoft.po.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import com.neusoft.po.Currency;
import com.neusoft.service.CurrencyService;

import javax.ws.rs.Path;

@RestController
@RequestMapping("/CurrencyController")
@RefreshScope
public class CurrencyController {
	@Autowired
	CurrencyService currencyService;
	
	//create a currency record for new users 
	//only can be called when a userId not in database(can only call once for each user)
	@PostMapping("/saveCurrency/{userId}/{amount}")
	public CommonResult<Integer> saveCurrency(@PathVariable String userId,
											  @PathVariable Double amount) throws Exception{
//		Currency param = new Currency();
//		param.setUserId(userId);
//		param.setAmount(amount);
		int result = currencyService.saveCurrency(userId, amount);
		return new CommonResult<>(200, "success", result);
	}
	
	@GetMapping("/getCurrencyByUserId/{userId}")
	public CommonResult<Currency> getCurrencyByUserId(@PathVariable String userId) throws Exception{
		Currency result = currencyService.getCurrencyByUserId(userId);
		return new CommonResult<>(200, "success", result);

	}
	
	@PutMapping("/addCurrencyByUserId/{addAmount}/{userId}")
	public CommonResult<Integer> addCurrencyByUserId(@PathVariable("addAmount") Double addAmount,
													 @PathVariable("userId") String userId)throws Exception{
		System.out.println("controller: " + addAmount + " " + userId);
		int result = currencyService.addCurrencyByUserId(addAmount, userId);
		return new CommonResult<>(200, "success", result);

	}
	
	@PutMapping("/minusCurrencyByUserId/{minusAmount}/{userId}")
	public CommonResult<Integer> minusCurrencyByUserId(@PathVariable Double minusAmount,
													   @PathVariable String userId) {
		int result = currencyService.minusCurrencyByUserId(minusAmount, userId);
		return new CommonResult<>(200, "success", result);
	}
}
